﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class findAllorOneStudentInfo : Form
    {
        public findAllorOneStudentInfo()
        {
            InitializeComponent();
        }

        private void btn_findOne_Click(object sender, EventArgs e)
        {

            string id = txt_id.Text;
            if (string.IsNullOrEmpty(id))
            {
                MessageBox.Show("请填入id查询");
                return;
            }
            string sql = "select * from student where stuno =" + id;
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
            //DataSet ds = new DataSet();
            //DBAccess.DataAdapter.Fill(ds);
            
           // dataGridView1.Columns[0].ReadOnly = true;
          
        }

        private void btn_findAll_Click(object sender, EventArgs e)
        {
            string sql = "select * from student";
            DataTable dt = DBAccess.GetTable(sql);  
            dataGridView1.DataSource = dt;
           
            //DataSet ds = new DataSet();
            //DBAccess.DataAdapter.Fill(ds);
            //dataGridView1.Columns[0].ReadOnly = true;
        }

        private void btn_delStu_Click(object sender, EventArgs e)
        {
            
            //int row = dataGridView1.CurrentCellAddress.Y + 1;
            //int col = dataGridView1.CurrentCellAddress.X + 1;
            //string value = dataGridView1.Rows[row - 1].Cells[col - 1].Value.ToString();
            int res = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 1; j <=5; j++)
                {
                    string colo = this.dataGridView1.Columns[this.dataGridView1.Rows[i].Cells[j].ColumnIndex].HeaderText;
                    string sql = "update student set " + colo + "=@value where stuno=@id";
                    SqlParameter[] pArr = { new SqlParameter("@value", dataGridView1.Rows[i].Cells[j].Value.ToString()), new SqlParameter("@id", dataGridView1.Rows[i].Cells[0].Value.ToString()) };
                    res = DBAccess.GetState(sql, pArr);
                }
            }
            if (res!=0)
            {
                MessageBox.Show("修改成功");
                findAllorOneStudentInfo_Load(sender,e);
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }
           

        private void btn_update_Click(object sender, EventArgs e)
        {
            //btn_findAll_Click(sender, e);
            //if (dbUpdate())
            //{
            //    MessageBox.Show("修改成功！");
            //}
            //else
            //{
            //    MessageBox.Show("修改失败");
            //}
            //string oldId = txt_id.Text;
            //string newId = txt_newId.Text;
            //string sql = "update student set stuno=@newId where stuno =@oldId";
            //string sql = "update student set stuno="+newId+" where stuno ="+oldId;
            //SqlParameter[] sqlarray = { new SqlParameter("@newId", newId), new SqlParameter("@oldId", oldId) };
            //object res = DBAccess.GetState(sql,sqlarray);
            //int res = DBAccess.GetState(sql);
            //if (res != 0)
            //{
            //    MessageBox.Show("更新成功");
            //    btn_findOne_Click(sender, e);
            //}
            //else
            //{
            //    //MessageBox.Show(sql);
            //    MessageBox.Show("更新失败");
            //}
            //DataTable dt = DBAccess.GetTable(sql,sqlarray);
            //dataGridView1.DataSource = dt;
        }

        private void findAllorOneStudentInfo_Load(object sender, EventArgs e)
        {
            string sql = "select * from student";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = txt_id.Text;
            if (string.IsNullOrEmpty(id))
            {
                MessageBox.Show("请填入id查询");
                return;
            }
            string sql = "select *  from student where stuno like '%" + id+"%'";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col = dataGridView1.CurrentCellAddress.X + 1;
            txt_id.Text = dataGridView1.Rows[row - 1].Cells[0].Value.ToString();
        }
        //int intindex = 0;
        //private bool dbUpdate()
        //{
        //    string sql = "select * from student";
        //    DataTable updateTable = new DataTable();
        //    updateTable = DBAccess.GetTable(sql);
        //    updateTable.Clear();
        //    DataTable dt = DBAccess.GetTable(sql);
        //    dt = (DataTable)this.dataGridView1.DataSource;
        //    dt.ImportRow(dt.Rows[intindex]);
        //    this.Update();
        //    dt.AcceptChanges();
        //    return true;
        //}
        //private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    intindex = e.RowIndex;
        //}
    }
}
